package org.djw.pet.controller;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.djw.basic.exception.BusinessException;
import org.djw.basic.util.AjaxResult;
import org.djw.basic.util.LoginContext;
import org.djw.basic.util.PageList;
import org.djw.pet.domain.Pet;
import org.djw.pet.domain.SearchMasterMsg;
import org.djw.pet.query.SearchMasterMsgQuery;
import org.djw.pet.service.ISearchMasterMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private ISearchMasterMsgService searchMasterMsgService;

    @PutMapping("/handle")
    public AjaxResult handle(@RequestBody Pet pet, HttpServletRequest request){
        try {
            searchMasterMsgService.handle(pet, LoginContext.getCurrentLogininfo(request));
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,请稍后重试!");
        }
    }

    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request){
        try {
            searchMasterMsgService.publish(searchMasterMsg, LoginContext.getCurrentLogininfo(request));
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,请稍后重试!");
        }
    }

    /**
     * 用户查询自己的寻主消息
     * @param query
     * @param request
     * @return
     */
    @PostMapping("/user")
    public PageList<SearchMasterMsg> user(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request){
        return searchMasterMsgService.user(query,LoginContext.getCurrentLogininfo(request));
    }

    /**
     * 查询待处理寻主消息
     * @param query
     * @param request
     * @return
     */
    @PostMapping("/pending")
    public PageList<SearchMasterMsg> pending(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        return searchMasterMsgService.pending(query,LoginContext.getCurrentLogininfo(request));
    }

    /**
     * 查询已处理寻主消息
     * @param query
     * @param request
     * @return
     */
    @PostMapping("/processed")
    public PageList<SearchMasterMsg> processed(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        return searchMasterMsgService.processed(query,LoginContext.getCurrentLogininfo(request));
    }
}
